import { ajax, tools } from './ajax.js';

//#region 手机验证码
let txt_imageCode = document.getElementById('txt_imageCode');
let img_code = document.getElementById('img_code');
let btn_imgcode = document.getElementById('btn_imgcode');
let txt_phone = document.getElementById('txt_phone');
let btn_code = document.getElementById('btn_code');

//获取图片验证码
function getCodeImg() {
  ajax.post('/tool/getImageCode', {}, (data) => {
    if (data.success) {
      img_code.setAttribute('src', data.message);
      return;
    }
    showToast(data.message);
  });
}

getCodeImg();

img_code.addEventListener('click', getCodeImg);
btn_imgcode.addEventListener('click', getCodeImg);

btn_code.addEventListener('click', () => {
  ajax.post(
    '/tool/sendValidateCode',
    {
      phone: txt_phone.value,
      imageCode: txt_imageCode.value,
    },
    (data) => {
      showToast(data.message);
    }
  );
});
//#endregion

//#region 弹出层提升框

let liveToast = document.getElementById('liveToast');
const toast = new bootstrap.Toast(liveToast);
//#liveToast . toast-body
//querySelector是通过css选择器获取页面上的元素（只会返回一个）
const toastBody = document.querySelector('#liveToast .toast-body');
console.log('toastBady===>', toastBody);
function showToast(message) {
  toastBody.innerHTML = message;
  toast.show();
}
//#endregion
